﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System.Windows;
using System.Collections.ObjectModel;
using Shoozla.DataModel;
using Shoozla.Helper;
using System.Net;
using System.IO;
using System.Drawing;
using System.Diagnostics;


namespace Shoozla.ViewModel
{
    public class PreviewViewModel : ViewModelBase
    {

        public PreviewViewModel()
        {
            
        }

        #region Commands

        private RelayCommand _CmdApply;
        public RelayCommand CmdApply
        {
            get
            {
                if (_CmdApply == null)
                {
                    _CmdApply = new RelayCommand(param => DoCmdApply(), param => (CanExecuteSelectedItemsOp() & CanExecuteUI()));
                }
                return _CmdApply;
            }
        }
       


        #endregion

        #region Do Commands
        private void DoCmdApply()
        {
            //clean the logging
            App.Logger.FilesSaved = 0;
            App.ThreadingFactor = App.ThreadingFactorEnum.Single;
            App.CoverWorker = new ListWorker<Mp3File>(SelectedItems.ToList<Mp3File>(), App.CalculateTreads(SelectedItems.Count));
            RaiseItemOperationStarted(SelectedItems.Count,"Saving covers");
            App.CurrenAppStatus = App.AppStatus.ApplyingCovers;
            App.CoverWorker.SplitAndRun(CoverApplyerWorker);      
        }

        private void CoverApplyerWorker(object o)
        {
           
                List<Mp3File> workingList = o as List<Mp3File>;
                if (workingList != null)
                {
                    IMP3Processor mp3Processor = new IDSharpProcessor();
                    foreach (Mp3File song in workingList)
                    {
                        try
                        {
                            // Mp3File song = mp3Processor.ReadID3Tag(c.FileName);
                            if (song.ImageUrl == null)
                                continue;

                            WebRequest req = WebRequest.Create(song.ImageUrl);
                            WebResponse response = req.GetResponse();
                            Stream stream = response.GetResponseStream();
                            song.Cover = Image.FromStream(stream);
                            bool res = mp3Processor.SaveID3Tag(song);
                            if (res)
                            {
                                App.LogEntry("[SUCCESS] " + song + "  - Cover Saved");
                                RaiseFileSaved(song.FileName);
                                RaiseItemRead(true);
                                continue;
                            }
                            else
                                RaiseItemRead(false);
                        }
                        catch (Exception e)
                        {
                            App.LogEntry("[ERROR] CoverApplyerWorker: " + e.Message);
                            continue;
                        }
                    }
                }
           
        }

        #endregion
    }
}
